/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import data.Connect;
import data.Constant;
import data.UserInfo;
import data.List_User;
import data.User_Update;
import data.List_DefTablespace;
import data.List_TmpTablespace;
/**
 *
 * @author NS
 */
@WebServlet(name = "Update", urlPatterns = {"/Update"})
public class user_Update extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            
            
            String ID = request.getParameter("ID");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String defTbs   = request.getParameter("defTbls");
            String tmpTbs   = request.getParameter("tmpTbls");
            String accStt   = request.getParameter("status");
            String quota    = request.getParameter("quota");
            String profile  = request.getParameter("profileList");
            
            
            //Connect to database
            Connect conn = new Connect();
            String mess = conn.connectData(Constant.OracleAdminUsr, Constant.OracleAdminPwd);
            
            if(!mess.equals("success")){
                out.println("<h3><font color = #FF0000>"+mess+"</font></h3>");
                return;
            }
            
            //Add new information for this user
            UserInfo usrInf = new UserInfo();
            usrInf.setID(Integer.parseInt(ID));
            usrInf.setUsername(username);
            usrInf.setPwd(password);
            usrInf.setDefTbspace(defTbs);
            usrInf.setTmpTbspace(tmpTbs);
            usrInf.setAccStatus(accStt);
            usrInf.setQuota(quota);
            usrInf.setProfile(profile);
            
            
            //Add new user to table
            User_Update user = new User_Update(conn.getConnection());
            String udMess = user.Update(usrInf);
            
            if(!udMess.equals("success")){
                out.println(udMess);
                return;
            }
            //Current user
            UserInfo inf = (UserInfo) session.getAttribute("user");
            //Update the list
            List_User l = new List_User(conn.getConnection());
            ArrayList<UserInfo> list = l.queryUserList(inf.getUsername());
            
            //Update default tablespace
            List_DefTablespace def = new List_DefTablespace(conn.getConnection());
            ArrayList<String> defLst = def.tblspaceLst();
            
            //Update temporary tablespace
            List_TmpTablespace tmp = new List_TmpTablespace(conn.getConnection());
            ArrayList<String> tmpLst = tmp.tbspaceList();
            
            //Set attribute
            if(username.equals(inf.getUsername())){
                session.setAttribute("user", usrInf);
            }
            
            session.setAttribute("userList", list);
            session.setAttribute("defTbsList", defLst);
            session.setAttribute("tmpTbsList", tmpLst);
                    
            //Dispatch 
            String url = "/UserInfo.jsp";
            RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
            
        } catch (Exception e){
            out.println(e);
        }
        
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
